<template>
  <view class="main_box">
    <view class="loginMain">
      <image class="company_icon" :src="icon" />
      <view class="nh"> 您好 </view>
      <view class="hy"> 欢迎使用 </view>
      <view class="form">
        <input placeholder="请输入用户" v-model="account" maxlength="11" />
        <input
          placeholder="请输入密码"
          type="password"
          v-model="passs"
          :password="true"
        />
      </view>
      <view class="loginBtn" @click="logins"> 登录 </view>
    </view>
  </view>
</template>
<script>
import jsencrypt from "@/js_sdk/jsencrypt-Rsa/jsencrypt/jsencrypt.vue";
// 密钥对生成 http://web.chacuo.net/netrsakeypair
import { getappAppProductTasks } from "../../api/public.js";
const publiukey =
  "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n" +
  "2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ==";

import { loginH5 } from "../../api/user.js";
export default {
  data: function () {
    return {
      account: "", //admin
      passs: "", //a123456
      spread: "",
      set_value: "",
      shows: false,
      pubblicData: "",
      stayAtLogin: false,
      icon: require("@/static/images/login_icon.png"),
    };
  },

  onShow() {
    let that = this;
    let judgeIsLogedOut = null;
    let user_info = uni.getStorage({
      key: "user_info",
    });
    try {
      const value = uni.getStorageSync("log_out");
      if (value) {
        console.log(value);
        judgeIsLogedOut = value;
      }
    } catch (e) {
      // error
    }

    console.log(user_info);
  },
  created() {
    this.account = uni.getStorageSync("username");
    this.passs = uni.getStorageSync("password");
  },
  methods: {
    sets() {
      this.shows = true;
    },
    close() {
      this.shows = false;
    },
    writes() {
      let that = this;
      if (that.set_value) {
        that.$store.state.app.baseUrls = that.set_value;
        uni.setStorage({
          key: "HTTP_REQUEST_URL",
          data: that.set_value,
        });
        try {
          uni.setStorageSync("HTTP_REQUEST_URL", that.set_value);
        } catch (e) {
          // error
        }
        // localStorage.setItem("HTTP_REQUEST_URL", that.set_value);
        // var r
        // r=localStorage.getItem("HTTP_REQUEST_URL");
        // console.log('缓存中的值为',r)
        that.shows = false;
      } else {
        uni.showToast({
          title: "请输入服务器地址",
          icon: "none", //不要图标
          duration: 2000, //1后消失
        });
      }
    },
    jumpToHome() {
      uni.switchTab({
        url: "/pages/home/index",
      });
    },
    logins() {
      // console.log(13132)
      let that = this;
      if (!that.account) {
        uni.showToast({
          title: "请输入用户",
          icon: "none", //不要图标
          duration: 2000, //1后消失
        });
        return;
      }

      if (!that.passs) {
        uni.showToast({
          title: "请输入密码",
          icon: "none", //不要图标
          duration: 2000, //1后消失
        });
        return;
      }

      this.pubblicData = jsencrypt.setEncrypt(publiukey, that.passs);
      uni.showLoading({
        title: "登录中",
      });
      loginH5({
        username: this.account,
        password: this.pubblicData,
        code: this.spread,
        uuid: "",
      })
        .then((res) => {
          uni.hideLoading();
          if (res.status != 400) {
            console.log(res);
            that.$store.commit("LOGIN", {
              token: res.token,
              time: new Date().getTime(),
            });

            uni.setStorageSync("username", that.account);
            uni.setStorageSync("password", that.passs);
            try {
              uni.setStorageSync("user_info", res.user);
            } catch (e) {
              // error
            }
            try {
              uni.setStorageSync("log_out", false);
            } catch (e) {
              // error
            }

            uni.showToast({
              title: "登录成功",
              icon: "none", //不要图标
              duration: 2000, //1后消失
            });

            uni.switchTab({
              url: "../home/index",
            });
          } else if (res.status == 404) {
            uni.showToast({
              title: "请配置好服务器",
            });
          } else {
            if (res.message === "Bad credentials") {
              uni.showToast({
                title: "密码错误",
                icon: "none", //不要图标
                duration: 3000, //1后消失
              });
            } else {
              uni.showToast({
                title: res.message,
                icon: "none", //不要图标
                duration: 3000, //1后消失
              });
            }
          }
        })
        .catch((e) => {
          uni.hideLoading();
          uni.showToast({
            title: e.msg,
            icon: "none", //不要图标
            duration: 3000, //1后消失
          });
        });
    },
    //监听键盘回车键
    search() {
      this.logins();
    },
  },
};
</script>
<style lang="scss" scoped>
.main_box {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: 30%;
  align-items: center;

  background-image: url("http://129.211.211.126/download/loginBg.png");
  background-size: cover;
  background-repeat: repeat;
  .loginMain {
    width: 650rpx;
    .nh {
      margin-top: 30rpx;
      font-size: 60rpx;
      height: 100rpx;
      color: #333333;
    }
    .hy {
      font-size: 50rpx;
      height: 100rpx;
      color: #333333;
      font-weight: lighter;
    }
    .company_icon {
      width: 80rpx;
      height: 80rpx;
    }
    input {
      background: white;
      height: 90rpx;
      width: 100%;
      border-radius: 30rpx;
      margin-bottom: 30rpx;
      padding-left: 30rpx;
    }
    .loginBtn {
      background: #9db6f3;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      height: 90rpx;
      border-radius: 40rpx;
    }
  }
}
</style>
